package com.jdcjk.project.car.view.gaode;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.LinearInterpolator;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.ScaleAnimation;
import com.jdcjk.R;
import com.jdcjk.project.main.model.entity.CarWarningInfo;
import com.jdcjk.util.GaoDeMapUtil;

/**
 * Created by Administrator on 2017/1/18 0018.
 */

public class GDMapCarWarningDialog extends DialogFragment {
    private float zoomLevel = 17;
    private TextureMapView mapView;
    private AMap aMap;
    private CarWarningInfo carWarningInfo;
    private LatLng warningLatlng;
    private Marker warningMarker;


    public GDMapCarWarningDialog() {
    }

    /**
     * 车辆报警位置
     *
     * @param carWarningInfo 车辆报警信息
     */
    public void setCarWarningInfo(CarWarningInfo carWarningInfo) {
        this.carWarningInfo = carWarningInfo;
        warningLatlng = GaoDeMapUtil.gaodeLatLngEncrypt(new LatLng(carWarningInfo.getLatitude(), carWarningInfo.getLongitude()));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewContent = inflater.inflate(R.layout.dialog_warning_gaode_map, container, false);
        //去掉dialog默认标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mapView = (TextureMapView) viewContent.findViewById(R.id.car_warning_map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        initMap();
        return viewContent;
    }

    //初始化地图
    private void initMap() {
        //初始化地图变量
        if (aMap == null) aMap = mapView.getMap();

        // 获取Ui设定
        UiSettings mUiSettings = aMap.getUiSettings();
        // 设置显示缩放控件
        mUiSettings.setScaleControlsEnabled(true);
        // 设置不显示指南针控件
        mUiSettings.setCompassEnabled(false);
        // 设置所有手势可用
        mUiSettings.setAllGesturesEnabled(true);
        //设定地图缩放等级
        aMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
        //定义了当可视范围改变时回调的接口。
        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                //在可视范围改变完成之后回调此方法。
                zoomLevel = cameraPosition.zoom;
            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
            }
        });
        addWarningMarker(warningLatlng);
    }

    /**
     * 在对应位置添加报警标志物
     *
     * @param latLng 位置信息
     * @return
     */
    private Marker addWarningMarker(LatLng latLng) {
        // 创建标注物
        MarkerOptions markerOption = new MarkerOptions();
        // 设置地点位置（经纬度）
        markerOption.position(latLng);
        // 设置标注物的标题和描述--坑爹，必须设置
        markerOption.title(carWarningInfo.getDisplayid()).snippet(carWarningInfo.getWarningList().get(0));
        // 设置标注物是否可以拖拽（长按拖拽）
        markerOption.draggable(true);
        //判断当前车辆方向
        // 设置标注物的自定义图标
        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ico_car_warning)));//图标
//        // 添加标注物至地图显示
        if (warningMarker != null) {
            warningMarker.remove();//清除上一个标志物
        }
        warningMarker = aMap.addMarker(markerOption);
        //移动到车辆位置
        animateToLocation(latLng);
        setMarkerAnimation(latLng);
        return warningMarker;
    }

    /**
     * 将中心移动到对应坐标
     *
     * @param latLng 坐标信息
     */
    private CameraPosition cameraPosition;

    public void animateToLocation(LatLng latLng) {
        aMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition.fromLatLngZoom(latLng, zoomLevel)));
    }


    /**
     * 启动标志物动画
     */
    public void setMarkerAnimation(LatLng latLng) {
        Animation animation = new ScaleAnimation(0, 1, 0, 1);
        animation.setInterpolator(new LinearInterpolator());
        //整个移动所需要的时间
        animation.setDuration(1000);
        //设置动画
        warningMarker.setAnimation(animation);
        //开始动画
        warningMarker.startAnimation();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

}
